package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xspace.android.xafcommon.been.BottomViewInfo;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.List;


public class AppMenusDBBiz extends BaseSql {

    private String appTable = "menuinfo";

    private String menuName = "menuName";
    private String packageName = "packageName";
    private String menuImg = "menuImg";
    private String menuImgSelected = "menuImgSelected";
    private String menuUrl = "menuUrl";
    private String menuKey = "menukey";
    private String isAndroidApp = "isAndroidApp";
    private String isShowOnHomePage = "isShowOnHomePage";
    private String isInstall = "isInstall";
    private String date = "date";
    private String menuIndex = "menuIndex";
    private String score = "score";

    public AppMenusDBBiz(MyDBHelper db) {
        super(db);
    }

    public AppMenusDBBiz() {

    }

    @Override
    public void create(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + appTable
                + " (" + menuName + " text PRIMARY  KEY,"
                + packageName + " text ,"
                + menuImg + " text,"
                + menuImgSelected + " text,"
                + menuUrl + " text,"
                + menuKey + " text,"
                + isAndroidApp + " text,"
                + isShowOnHomePage + " text,"
                + isInstall + " text,"
                + date + " text,"
                + menuIndex + " text,"
                + score + " text"
                + ");");

    }


    public void dropTable(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + appTable);

    }

    @Override
    public void upgrade(SQLiteDatabase db) {
    }

    /**
     * 保存sort数据到本地数据库
     */
    public int saveApp(BottomViewInfo botInfo) {
        where = packageName + "='" + botInfo.packageName
                + "' and " + menuName + "='" + botInfo.title
                + "' and " + menuImg + "='" + botInfo.image
                + "' and " + menuImgSelected + "='" + botInfo.menuImgSelected
                + "' and " + menuUrl + "='" + botInfo.url
                + "' and " + menuKey + "='" + botInfo.key
                + "' and " + isAndroidApp + "='" + botInfo.isAndroidApp
                + "' and " + date + "='" + botInfo.date
                + "' and " + menuIndex + "='" + botInfo.menuIndex + "'";

        values = new ContentValues();
        values.put(menuName, botInfo.title);
        values.put(packageName, botInfo.packageName);
        values.put(menuImg, botInfo.image);
        values.put(menuImgSelected, botInfo.menuImgSelected);
        values.put(menuUrl, botInfo.url);
        values.put(menuKey, botInfo.key);
        values.put(isAndroidApp, botInfo.isAndroidApp);
        values.put(isShowOnHomePage, botInfo.isShowOnHomePage);
        values.put(isInstall, botInfo.isInstall);
        values.put(date, botInfo.date);
        values.put(menuIndex, botInfo.menuIndex);
        values.put(score, botInfo.score);
        LogUtil.d("TrustDBBiz==添加=" + botInfo.packageName);
        return super.saveOrUpdate(appTable);
    }

    public List<BottomViewInfo> queryAndroidApp() {
        where = " isAndroidApp =1";
        List<BottomViewInfo> list = new ArrayList<BottomViewInfo>();
        try {
            db.open();
            Cursor cur = db.query(appTable, where);
            if (cur != null) {
                for (int i = 0; i < cur.getCount(); i++) {
                    if (cur.moveToPosition(i)) {
                        BottomViewInfo bean = new BottomViewInfo();
                        bean.title = cur.getString(0);
                        bean.packageName = cur.getString(1);
                        bean.image = cur.getString(2);
                        bean.menuImgSelected = cur.getString(3);
                        bean.url = cur.getString(4);
                        bean.key = cur.getString(5);
                        bean.isAndroidApp = cur.getString(6);
                        bean.isShowOnHomePage = cur.getString(7);
                        bean.isInstall = cur.getString(8);
                        bean.date = cur.getString(9);
                        bean.menuIndex = cur.getString(10);
                        bean.score = cur.getString(11);
                        list.add(bean);
                        LogUtil.d("TrustDBBiz==查询=" + bean.title);
                    }
                }
            }
            cur.close();
            db.close();
        } catch (Exception e) {
            LogUtil.d("TrustDBBiz==查询异常=" + e.toString());
            e.printStackTrace();
        }
        return list;
    }

    public List<BottomViewInfo> queryUnAndroidApp() {
        where = " isAndroidApp =0";
        List<BottomViewInfo> list = new ArrayList<BottomViewInfo>();
        try {
            db.open();
            Cursor cur = db.query(appTable, where);
            if (cur != null) {
                for (int i = 0; i < cur.getCount(); i++) {
                    if (cur.moveToPosition(i)) {
                        BottomViewInfo bean = new BottomViewInfo();
                        bean.title = cur.getString(0);
                        bean.packageName = cur.getString(1);
                        bean.image = cur.getString(2);
                        bean.menuImgSelected = cur.getString(3);
                        bean.url = cur.getString(4);
                        bean.key = cur.getString(5);
                        bean.isAndroidApp = cur.getString(6);
                        bean.isShowOnHomePage = cur.getString(7);
                        bean.isInstall = cur.getString(8);
                        bean.date = cur.getString(9);
                        bean.menuIndex = cur.getString(10);
                        bean.score = cur.getString(11);
                        list.add(bean);
                        LogUtil.d("TrustDBBiz==查询=" + bean.title);
                    }
                }
            }
            cur.close();
            db.close();
        } catch (Exception e) {
            LogUtil.d("TrustDBBiz==查询异常=" + e.toString());
            e.printStackTrace();
        }
        return list;
    }

    public boolean isAppInTable(String name) {
        boolean isExit = false;
        where = " menuName='" + name + "'";
        try {
            db.open();
            Cursor cur = db.query(appTable, where);
            if (cur != null) {
                if (cur.getCount() > 0) {
                    isExit = true;
                }
            }
            cur.close();
            db.close();
        } catch (Exception e) {
            LogUtil.d("TrustDBBiz==查询异常=" + e.toString());
            e.printStackTrace();
        }
        return isExit;
    }

    /**
     * 查询保存的sort信息
     */
    public List<BottomViewInfo> queryApp() {
        where = " isShowOnHomePage =1";
        List<BottomViewInfo> list = new ArrayList<BottomViewInfo>();
        try {
            db.open();
            Cursor cur = db.query(appTable, where);
            if (cur != null) {
                for (int i = 0; i < cur.getCount(); i++) {
                    if (cur.moveToPosition(i)) {
                        BottomViewInfo bean = new BottomViewInfo();
                        bean.title = cur.getString(0);
                        bean.packageName = cur.getString(1);
                        bean.image = cur.getString(2);
                        bean.menuImgSelected = cur.getString(3);
                        bean.url = cur.getString(4);
                        bean.key = cur.getString(5);
                        bean.isAndroidApp = cur.getString(6);
                        bean.isShowOnHomePage = cur.getString(7);
                        bean.isInstall = cur.getString(8);
                        bean.date = cur.getString(9);
                        bean.menuIndex = cur.getString(10);
                        bean.score = cur.getString(11);
                        list.add(bean);
                        LogUtil.d("TrustDBBiz==查询=" + bean.title);
                    }
                }
            }
            cur.close();
            db.close();
        } catch (Exception e) {
            LogUtil.d("TrustDBBiz==查询异常=" + e.toString());
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 查询保存的sort信息
     */
    public List<BottomViewInfo> queryAllApp() {
        List<BottomViewInfo> list = new ArrayList<BottomViewInfo>();
        try {
            db.open();
            Cursor cur = db.query(appTable, where);
            if (cur != null) {
                for (int i = 0; i < cur.getCount(); i++) {
                    if (cur.moveToPosition(i)) {
                        BottomViewInfo bean = new BottomViewInfo();
                        bean.title = cur.getString(0);
                        bean.packageName = cur.getString(1);
                        bean.image = cur.getString(2);
                        bean.menuImgSelected = cur.getString(3);
                        bean.url = cur.getString(4);
                        bean.key = cur.getString(5);
                        bean.isAndroidApp = cur.getString(6);
                        bean.isShowOnHomePage = cur.getString(7);
                        bean.isInstall = cur.getString(8);
                        bean.date = cur.getString(9);
                        bean.menuIndex = cur.getString(10);
                        bean.score = cur.getString(11);
                        list.add(bean);
                        LogUtil.d("TrustDBBiz==查询=" + bean.title);
                    }
                }
            }
            cur.close();
            db.close();
        } catch (Exception e) {
            LogUtil.d("TrustDBBiz==查询异常=" + e.toString());
            e.printStackTrace();
        }
        return list;

    }


    public List<BottomViewInfo> queryMoreApp() {
        where = " isShowOnHomePage =0";
        List<BottomViewInfo> list = new ArrayList<BottomViewInfo>();
        try {
            db.open();
            Cursor cur = db.query(appTable, where);
            if (cur != null) {
                for (int i = 0; i < cur.getCount(); i++) {
                    if (cur.moveToPosition(i)) {
                        BottomViewInfo bean = new BottomViewInfo();
                        bean.title = cur.getString(0);
                        bean.packageName = cur.getString(1);
                        bean.image = cur.getString(2);
                        bean.menuImgSelected = cur.getString(3);
                        bean.url = cur.getString(4);
                        bean.key = cur.getString(5);
                        bean.isAndroidApp = cur.getString(6);
                        bean.isShowOnHomePage = cur.getString(7);
                        bean.isInstall = cur.getString(8);
                        bean.date = cur.getString(9);
                        bean.menuIndex = cur.getString(10);
                        bean.score = cur.getString(11);
                        list.add(bean);
                        LogUtil.d("TrustDBBiz==查询=" + bean.title);
                    }
                }
            }
            cur.close();
            db.close();
        } catch (Exception e) {
            LogUtil.d("TrustDBBiz==查询异常=" + e.toString());
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 删除保存的sort信息
     */
    public int deleteAllSort() {
        selection = null;
        return super.delete(appTable);
    }

    /**
     * 删除保存的sort信息
     */
    public int deleteMenus(String mWhere) {

        selection = menuName + "='" + mWhere + "'";
        LogUtil.d("TrustDBBiz==删除=" + mWhere);
        return super.delete(appTable);
    }

    /**
     * 删除保存的sort信息
     */
    public int deleteUnAndroidApp() {

        selection = " isAndroidApp = 0";
        return super.delete(appTable);
    }

}
